package com.alienlab.finder.web.rest;
import com.alienlab.finder.domain.Warning;
import com.alienlab.finder.service.WarningService;
import com.alienlab.finder.service.dto.WarningDTO;
import com.alienlab.finder.web.rest.errors.BadRequestAlertException;
import com.alienlab.finder.web.rest.util.HeaderUtil;
import com.alienlab.finder.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Warning.
 */
@RestController
@RequestMapping("/api")
public class WarningResource {

    private final Logger log = LoggerFactory.getLogger(WarningResource.class);

    private static final String ENTITY_NAME = "warning";

    private final WarningService warningService;

    public WarningResource(WarningService warningService) {
        this.warningService = warningService;
    }



    @ApiOperation(value = "根据任务id获取警告信息")
    @GetMapping("/warnings/taskid")
    public ResponseEntity  getWarningListBytaskId(@RequestParam String taskid ) {
        log.debug("REST request to get 根据任务id获取警告信息");
        try {
            WarningDTO warningDTO = warningService.getWarningMessageByTaskId(taskid);
            return ResponseEntity.ok(warningDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());

        }
    }

    @ApiOperation(value = "根据admin_open_id获取警告信息列表")
    @GetMapping("/warnings/adminopenid")
    public ResponseEntity  getWarningListByAdmin(@RequestParam String admin_open_id ) {
        log.debug("REST request to get 根据admin_open_id获取警告信息列表");
        try {
            List<WarningDTO> warningDTOlist = warningService.getWarningList(admin_open_id);
            return ResponseEntity.ok(warningDTOlist);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());

        }
    }

    @ApiOperation(value = "修改警告信息为读取状态")
    @PutMapping("/warnings/readflag")
    public ResponseEntity updateWarningReadFlag(@RequestParam String warningid) throws URISyntaxException {
        log.debug("REST request to update修改警告信息为读取状态");

        Warning result = null;
        try {
            result = warningService.modifyReadState(Long.valueOf(warningid));
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());

        }
    }
    /**
     * POST  /warnings : Create a new warning.
     *
     * @param warning the warning to create
     * @return the ResponseEntity with status 201 (Created) and with body the new warning, or with status 400 (Bad Request) if the warning has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/warnings")
    public ResponseEntity<Warning> createWarning(@Valid @RequestBody Warning warning) throws URISyntaxException {
        log.debug("REST request to save Warning : {}", warning);
        if (warning.getId() != null) {
            throw new BadRequestAlertException("A new warning cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Warning result = warningService.save(warning);
        return ResponseEntity.created(new URI("/api/warnings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /warnings : Updates an existing warning.
     *
     * @param warning the warning to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated warning,
     * or with status 400 (Bad Request) if the warning is not valid,
     * or with status 500 (Internal Server Error) if the warning couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/warnings")
    public ResponseEntity<Warning> updateWarning(@Valid @RequestBody Warning warning) throws URISyntaxException {
        log.debug("REST request to update Warning : {}", warning);
        if (warning.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        Warning result = warningService.save(warning);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, warning.getId().toString()))
            .body(result);
    }

    /**
     * GET  /warnings : get all the warnings.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of warnings in body
     */
    @GetMapping("/warnings")
    public ResponseEntity<List<Warning>> getAllWarnings(Pageable pageable) {
        log.debug("REST request to get a page of Warnings");
        Page<Warning> page = warningService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/warnings");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /warnings/:id : get the "id" warning.
     *
     * @param id the id of the warning to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the warning, or with status 404 (Not Found)
     */
    @GetMapping("/warnings/id")
    public ResponseEntity<Warning> getWarning(@RequestParam String id) {
        log.debug("REST request to get Warning : {}", id);
        Optional<Warning> warning = warningService.findOne(Long.valueOf(id));
        return ResponseUtil.wrapOrNotFound(warning);
    }

    /**
     * DELETE  /warnings/:id : delete the "id" warning.
     *
     * @param id the id of the warning to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/warnings/{id}")
    public ResponseEntity<Void> deleteWarning(@PathVariable Long id) {
        log.debug("REST request to delete Warning : {}", id);
        warningService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
